<template>
    <div class="shopcar-container">

        <div class="mui-card" v-for="(value,index) in shopcar" :key="index">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <!--<div class="mui-switch mui-switch-blue mui-switch-mini">-->
                        <!--<div class="mui-switch-handle"></div>-->
                    <!--</div>-->
                    <!--v-model与store中的selected双向绑定-->
                    <mt-switch @change="$store.commit('changeGoodsSelected',{id:value.id,selected:$store.state.car[index].selected})" v-model="$store.state.car[index].selected"></mt-switch>
                    <img :src="value.src">
                    <div class="info">
                        <h1 class="title">{{value.title}}</h1>
                        <p>
                            <span class="price">{{value.price_now}}</span>
                            <numbox :count=$store.state.car[index].count :id="value.id" :max="value.max"></numbox>
                            <a @click.prevent="DelItem(index)">删除</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan">
                    <div class="left">
                        <p>总计（不含运费）</p>
                        <p>已勾选商品{{$store.getters.getSelectedCount}}<span class="red"></span> 件， 总价 <span class="red">￥{{$store.getters.getSelectedPrice}}</span></p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
            </div>
        </div>


    </div>

    <!--问题1：switch失效-->
</template>

<script>
    import numbox from '../subcomponents/shopcar_numbox.vue'
    export default {
        name: "shopcar-container",
        data(){
            return {
                shopcar:[],  //存的是goodslist中要购买的数组
            }
        },
        created(){
        //    通过id取购物车的数据
            this.getCarData()
        },
        methods:{
            getCarData(){
                this.$http.get('/api/goodslist').then(res=>{
                    //遍历store中的car，将每个ID对应的数据取出
                    this.$store.state.car.forEach(i=>{
                        this.shopcar.push(res.body.data[i.id-1])
                    })
                })
            },
            DelItem(index){//删除商品项
                this.$store.commit('delShopCarItem',index);
                this.shopcar.splice(index,1)
            },
        },
        components:{
            numbox
        }
    }
</script>

<style lang="scss" scoped>
    .shopcar-container {
        background-color: #eee;
        overflow: hidden;
        .mui-card-content-inner{
            display: flex;
            align-items: center;
        }
        img{
            width: 60px;
            height: 60px;
        }
        h1{
            font-size: 13px;
            width: 210px;
        }
        .info{
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            .title{//设置商品列表标题最多两行超出显示...
                height: 26px;
                overflow : hidden;
                display: -webkit-box;
                -webkit-line-clamp: 2;      /* 可以显示的行数*/
                text-overflow: ellipsis;       /*超出部分用...表示*/
                -webkit-box-orient: vertical;
            }
            p{
                margin: 0 0 4px 0;
                display: flex;
                justify-content: space-between;
                numbox{
                    padding: 0 5px;
                }
            }
            .price{
                color:red;
                font-weight: bold;
            }
        }
        .jiesuan {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .red {
                color: red;
                font-weight: bold;
                font-size: 16px;
            }
        }
    }
</style>